package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.BankCodeInfoMapper;
import cc.rengu.igas.bomp.common.entity.BankCodeInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.dbs.Database;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 联行行号表 操作实现类
 * <p>
 * Created by keyi on 2020/3/20.
 */
public class BankCodeInfoMapperImpl implements BankCodeInfoMapper {

    RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public BankCodeInfo bompGetBankCodeInfoByBankBranchCode(String bankBrancdCode) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "BompGetBankCodeInfoByBankBranchCode");

        BankCodeInfo opmsBankCodeInfo = new BankCodeInfo();
        opmsBankCodeInfo.setBankBranchCode(bankBrancdCode);

        int recode = dbsUtil.dbsExecuteSql(sqlStr, opmsBankCodeInfo, opmsBankCodeInfo);
        if (Database.DBS_NOT_FOUND == recode) {
            rglog.info("联行行号表不存在，bankBranchCode:<{}>", bankBrancdCode);
            return null;
        } else if (Database.DBS_SUCCESS != recode) {
            rglog.info("联行行号表查询失败，bankBranchCode:<{}>，recode:<{}>", bankBrancdCode, recode);
            return null;
        }

        return opmsBankCodeInfo;
    }

    @Override
    public List<BankCodeInfo> bompGetBankCodeInfoByCupsInstId(String cupsInstId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "BompGetBankCodeInfoByCupsInstId");

        BankCodeInfo bankCodeInfo = new BankCodeInfo();
        bankCodeInfo.setCupsInstId(cupsInstId);

        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, bankCodeInfo, BankCodeInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.info("联行行号表不存在，cupsInstId:<{}>", cupsInstId);
            return null;
        }
        return objectList.stream().map(item -> (BankCodeInfo) item).collect(Collectors.toList());
    }


    @Override
    public List<BankCodeInfo> selectBankCodeInfoByCondition(String bankName, String bankCode) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBankCodeInfoByCondition");
        BankCodeInfo bankCodeInfo = new BankCodeInfo();
        bankCodeInfo.setBankBranchCode(bankCode);
        bankCodeInfo.setBankName(bankName);
        bankCodeInfo.setBankBranchName(bankName);

        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, bankCodeInfo, BankCodeInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据行名行号查询人行信息表信息失败,bankName:<{}>,bankCode:<{}>,.", bankName, bankCode);
            return null;
        }
        return objectList.stream().map(item -> (BankCodeInfo) item).collect(Collectors.toList());
    }

    @Override
    public PageResult selectBankCodeInfoByCondition(String bankName, String bankCode, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBankCodeInfoByCondition");
        BankCodeInfo bankCodeInfo = new BankCodeInfo();
        bankCodeInfo.setBankBranchCode(bankCode);
        bankCodeInfo.setBankName(bankName);
        bankCodeInfo.setBankBranchName(bankName);

        PageResult pageResult = dbsUtil.dbsFetchPageListData(sqlStr, bankCodeInfo, BankCodeInfo.class, pageInfo);
        if (null == pageResult) {
            rglog.error("根据行名行号查询人行信息表信息失败,bankName:<{}>,bankCode:<{}>,.", bankName, bankCode);
            return null;
        }
        return pageResult;
    }
}
